package modules.majiang.data
{
	import com.netease.protobuf.UInt64;
	
	import flash.utils.Timer;
	
	import modules.lobby.data.NormalTableModel;
	import modules.majiang.effect.ChipsFromPotEffectData;
	import modules.majiang.effect.ChipsToPotEffectData;
	import modules.majiang.effect.DiscardEffectData;
	import modules.majiang.effect.FetchEffectData;
	import modules.majiang.viewData.ChatUIState;
	import modules.majiang.viewData.GameUIState;
	
	import protobuf.MSPKG_DICE_NTF;


	/**
	 * @author xumin.xu
	 * 保存桌子的所有数据
	 */
	public class TableDataModel
	{

		public static const GAME_TYPE_TILE_FOUR:int = 4;

		public static const GAME_TYPE_TILE_SEVEN:int = 7;

		/**
		 * 房间基本信息（在大厅中的信息）
		 **/
		[Bindable]
		public var tableDataFromLobby:NormalTableModel;

		/**
		 * 买入余额
		 **/
		[Bindable]
		public var money:Number;

		/**
		 * 手牌个数 (在没摸牌情况下手牌数)
		 * (游戏中手牌数分为 4张与7张)
		 */
		public var handTilesNum:int = GAME_TYPE_TILE_SEVEN;

		/**
		 * 玩家手牌
		 **/
		[Bindable]
		public var tilesInHand:Array;

		/**
		 * 桌子上的牌
		 **/
		public var tilesOnTable:Array;
		
		/**
		 * 牌局的ID,和游戏开始时间,用于服务器查询日志 
		 */		
		[Bindable]
		public  var paijuId:String = "0";
		
		[Bindable]
		public  var start_time:int;

		/**
		 * 坐下的玩家
		 * 数组中的类型为key:座位	value:TablePlayerDataModel
		 **/
		[Bindable]
		public var seatedPlayers:Array = [];

		/**
		 * 玩家自己所坐的位置
		 * -1表示不在座位上
		 */
		[Bindable]
		public var _sitChairId:int = -1;

		public function set sitChairId(value:int):void
		{
			_sitChairId = value;
		}

		public function get sitChairId():int
		{
			return _sitChairId;
		}

		/**
		 * 观看游戏的位置
		 * 玩家自己在玩游戏是该位置为自己所坐的位置
		 **/
		[Bindable]
		public var _lookChairId:int = 1;

		public function get lookChairId():int
		{
			return _lookChairId;
		}

		public function set lookChairId(value:int):void
		{
			_lookChairId = value;

			updateOwnDealerVisble();
		}

		/**
		 * 开始游戏倒计时
		 **/
		[Bindable]
		public var _matchStartCountDown:int = 0;
		public function get matchStartCountDown():int
		{
			return _matchStartCountDown;
		}
		
		public function set matchStartCountDown(value:int):void
		{
			_matchStartCountDown = value;
			updateUI();
		}
		
		/**
		 * 是否显示开始游戏倒计时
		 **/
		[Bindable]
		public var _matchStartCountDownVisible:Boolean = false;
		public function get matchStartCountDownVisible():Boolean
		{
			return _matchStartCountDownVisible;
		}
		public function set matchStartCountDownVisible(value:Boolean):void
		{
			_matchStartCountDownVisible = value;
			updateUI();
		}

		/**
		* 庄家座位号
		**/
		[Bindable]
		public var _bankerId:uint = 0;

		public function get bankerId():uint
		{
			return _bankerId;
		}

		public function set bankerId(value:uint):void
		{
			_bankerId = value;

			updateOwnDealerVisble();
		}

		/**
		 * 是否显示色子
		 **/
		[Bindable]
		public var showDice:Boolean = false;

		/**
		 * 麻将游戏的阶段(状态)(默认等待玩家状态)
		 * 使用setMatchState(value:int):void修改该值
		 **/
		[Bindable]
		public var matchState:int = MatchState.WAIT;

		/**
		 * 是否允许玩家出牌
		 **/
		[Bindable]
		public var disableDiscard:Boolean = false;

		/**
		 * 是否为锦标赛
		 */
		[Bindable]
		public var _isTournament:Boolean = false;

		public function set isTournament(value:Boolean):void
		{
			_isTournament = value;
		}

		public function get isTournament():Boolean
		{
			return _isTournament;
		}

		/**
		 * 锦标赛编号
		 **/
		private var _tournamentId:UInt64;

		public function set tournamentId(value:UInt64):void
		{
			_tournamentId = value;
		}

		public function get tournamentId():UInt64
		{
			return _isTournament ? _tournamentId : null;
		}

		/** 锦标赛类型 */
		public var tournamentType:int = 0;
		
		/**
		 * 设置麻将游戏阶段
		 **/
		public function setMatchState(value:int):void
		{
			matchState = value;

			//
			showDice = (matchState == MatchState.WAVE_PARTICLE);

			//更新游戏界面中买入按钮是否可见
			gameUIState.buyInBtnVisible = _sitChairId > -1 && matchState != MatchState.INGAME && !_isTournament;
		}

		/**
		 * 正在比赛的玩家列表
		 **/
		public var inMatchPlayerList:Array = [];

		/**
		* 赢牌玩家列表
		**/
		public var winPlayerList:Array = [];

		/**
		* 流局玩家列表
		**/
		public var drawPlayerList:Array = [];

		/**
		 * 输牌玩家列表
		 **/
		public var losePlayerList:Array = [];

		/**
		 * 弃牌玩家列表
		 **/
		public var foldPlayerList:Array = [];

		/**
		 * 离开玩家列表
		 **/
		public var sitOutPlayerList:Array = [];

		/**
		 * 除自己外的玩家手牌
		 **/
		public var playerTiles:Array = [null, [], [], [], [], [], []];

		/**
		* 保存游戏当前动作
		**/
		public var matchCurrentAction:MatchCurrentAction = new MatchCurrentAction();

		/**
		 * 保存本轮下注情况
		 **/
		public var epicycleBetInfos:Array = [];

		/**
		* 本局玩家总下注金额
		**/
		public var totalBetInGame:Array = [0, 0, 0, 0, 0, 0];

		/**
		 * 倒计时状态
		 **/
		public var countDownStates:Array = [null, new CountDownState(), new CountDownState(), new CountDownState(), new CountDownState(), new CountDownState(), new CountDownState()];

		/**
		 * 出牌特效数据
		 **/
		public var discardCardEffectData:DiscardEffectData;

		/**
		 * 发牌特效数据
		 **/
		public var fetchEffectData:FetchEffectData;
		
		/**
		 * 投赌注进入奖池数据源
		 **/
		public var chipsToPotEffectData:ChipsToPotEffectData;
		
		/**
		 * 赢牌时从奖池获得奖金动画数据
		 **/
		public var chipsFromPotEffectData:ChipsFromPotEffectData;

		/**
		 * gameUI状态
		 **/
		[Bindable]
		public var gameUIState:GameUIState = new GameUIState();

		/**
		 * chatUI状态
		 **/
		[Bindable]
		public var chatUIState:ChatUIState = new ChatUIState();
		
		/** 桌面上剩余牌堆数据 */
		[Bindable]
		public var restCardData:RestCardData;
		
		/**
		 * 奖池管理类
		 **/
		public var bonusPotsData:BonusPotsData;

		/**
		 * 计算出真实座位号
		 **/
		public function getChairIdByPosition(position:int):int
		{
			return (lookChairId + position + 12 - 2) % 6 + 1;
		}

		/**
		 * 根据椅子编号获取界面实际位置
		 **/
		public function getPositionByChairId(chairId:int):int
		{
			return (chairId - lookChairId + 12) % 6 + 1;
		}

		/**
		 * 判断某座位上的玩家是正在玩游戏
		 **/
		public function isInMatch(chairId:int):Boolean
		{
			var isInMatch:Boolean = inMatchPlayerList.filter(function(element:*, index:int, arr:Array):Boolean
			{
				return element.chairId == chairId;
			}).length > 0;
			return isInMatch;
		}

		/**
		 * 判断某座位上的玩家是否赢了
		 **/
		public function isWin(chairId:int):Boolean
		{
			var isWin:Boolean = winPlayerList.filter(function(element:*, index:int, arr:Array):Boolean
			{
				return element.chairId == chairId;
			}).length > 0;
			return isWin;
		}

		/**
		 * 判断某座位上的玩家是否流局
		 **/
		public function isDraw(chairId:int):Boolean
		{
			var isDraw:Boolean = drawPlayerList.filter(function(element:*, index:int, arr:Array):Boolean
			{
				return element.chairId == chairId;
			}).length > 0;
			return isDraw;
		}

		/**
		 * 判断某座位上的玩家是否输了
		 **/
		public function isLose(chairId:int):Boolean
		{
			var isLose:Boolean = losePlayerList.filter(function(element:*, index:int, arr:Array):Boolean
			{
				return element.chairId == chairId;
			}).length > 0;
			return isLose;
		}

		/**
		 * 判断某座位上的玩家是否弃牌了
		 **/
		public function isFold(chairId:int):Boolean
		{
			var isFold:Boolean = foldPlayerList.filter(function(element:*, index:int, arr:Array):Boolean
			{
				return element.chairId == chairId;
			}).length > 0;
			return isFold;
		}

		/**
		 * 判断某座位上的玩家是否离开
		 **/
		public function isSitout(chairId:int):Boolean
		{
			var isSitout:Boolean = sitOutPlayerList.filter(function(element:*, index:int, arr:Array):Boolean
			{
				return element.chairId == chairId;
			}).length > 0;
			return isSitout;
		}

		/**
		 * 更新观察者位置的庄家标志显示
		 **/
		public function updateOwnDealerVisble():void
		{
			var ownDealerVisible:Boolean = bankerId == lookChairId;
			//如果观看位置没有玩家时，庄家标志不应该显示
			if (seatedPlayers[lookChairId] == null)
			{
				ownDealerVisible = false;
			}
		}

		/**
		 * 默认对象
		 */
		public static var defaultTabledataModel:TableDataModel = new TableDataModel();

		/**
		 * 掷骰子庄家座位ID 协议数据
		 * 临时 保存 倒计时结束使用 见函数OnRecvMatchStartCoolDownNtf
		 **/
		public var recvDiceNtf:MSPKG_DICE_NTF;

		/** 开始倒计时 */
		public var countDownTimer:Timer;

		/**
		 * 是否显示等待信息
		 **/
		[Bindable]
		public var showWaitInfo:Boolean = false;

		public function TableDataModel()
		{
			fetchEffectData = new FetchEffectData(this);

			discardCardEffectData = new DiscardEffectData(this);

			bonusPotsData = new BonusPotsData(this);
			
			chipsToPotEffectData = new ChipsToPotEffectData(this)
			
			chipsFromPotEffectData = new ChipsFromPotEffectData(this);
		}
		
		/** 最大下注金额 */
		public var maxRaiseMoney:Number = Number.MAX_VALUE;
		
		private function updateUI():void
		{
//			UIAllRefer.instance.getGameUIbyTableId(tableDataFromLobby.tableID).tableUI.upDate();
		}
	}
}
